package cn.jekun.shop.common.service;

import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import cn.jekun.component.base.exceptions.BusinessException;
import cn.jekun.component.base.utils.SqlUtis;
import cn.jekun.component.base.validator.annotation.Security;
import cn.jekun.component.core.Constants;
import cn.jekun.component.core.bean.gen.DCity;
import cn.jekun.component.core.bean.gen.DProvince;
import cn.jekun.component.core.service.gen.DCityService;
import cn.jekun.component.core.service.gen.DProvinceService;
import cn.jekun.component.core.service.gen.ErpSupportCityService;
@Service(OpenedCityService.BEAN_NAME)
public class OpenedCityService extends ShopBaseService{
	public static final String BEAN_NAME = "cn.jekun.shop.common.service.OpenedCityService";
	@Resource(name = DCityService.BEAN_NAME)
	DCityService cityService;
	
	@Resource(name = DProvinceService.BEAN_NAME)
	DProvinceService dProvinceService;
	
	@Resource(name = ErpSupportCityService.BEAN_NAME)
	ErpSupportCityService erpSupportCityService;
	
	@Override
	public String getNamespace() {
		return "SHOP_OPENEDCITY";
	}
	/**
	 * 查询已开通地市
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public JSONObject queryCitys() {
		JSONObject jsonObject = new JSONObject();
		 List resultList = new ArrayList();
		 Map formMap = new HashMap();
		resultList = this.list(formMap);
		jsonObject.put("cityList",resultList);
		return executeRerutn(jsonObject);
	}
	/**
	 * 查询已开通地市下的区
	 * @param cityId
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public JSONObject queryRegion(@DCity.AnnoCityId String cityId){
		JSONObject jsonObject = new JSONObject();
		 List resultList = new ArrayList();
		resultList = this.listBySql("select * from d_region a where a.city_id='"+cityId+"'");
		jsonObject.put("regionList", resultList);
		return executeRerutn(jsonObject);
	}
	/**
	 * 通过cityName得到cityCode
	 * @param cityName
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public JSONObject getCityCodeByCityName(@Security String cityName) {
		if (StringUtils.isBlank(cityName)) {
			throw new BusinessException(Constants.ERRORS.VALIDATE_ERROR_CODE,
					"城市名不能为空");
		}
		JSONObject jsonObject = new JSONObject();
		Map map = new HashMap();
		map.put(DCity.COL_CITY_NAME, cityName);
		SqlUtis.prepareSql(map
				, SqlUtis.getSQLRequired(map,"a."+DCity.COL_CITY_NAME,Types.VARCHAR,SqlUtis.LIKE));
		DCity city = cityService.one(map);
		
		if(city == null){//找不到城市
			//throw new BusinessException(Constants.ERRORS.VALIDATE_ERROR_CODE,
			//		"该城市不存在");
			jsonObject.put("cityId", Constants.CITY_ALL);
			jsonObject.put("cityName", "全国");
			jsonObject.put("provinceId", "");
			jsonObject.put("provinceName", "");
		}else{
			Map erpSupportCityMap = new HashMap();
			erpSupportCityMap.put(DCity.COL_CITY_ID, city.getCityId());
			SqlUtis.prepareSql(erpSupportCityMap
					, SqlUtis.getSQL(erpSupportCityMap, DCity.COL_CITY_ID)
					);
			if(erpSupportCityService.exists(erpSupportCityMap)){//已经是开通城市
				jsonObject.put("cityId", city.getCityId());
				jsonObject.put("cityName", city.getCityName());
				DProvince dProvince = this.dProvinceService.one(city.getProvinceId());
				if (dProvince!=null) {
					jsonObject.put("provinceId", dProvince.getProvinceId());
					jsonObject.put("provinceName", dProvince.getProvinceName());
				}else {
					jsonObject.put("provinceId", "");
					jsonObject.put("provinceName", "");
				}
			}else{
				jsonObject.put("cityId", Constants.CITY_ALL);
				jsonObject.put("cityName", "全国");
				jsonObject.put("provinceId", "");
				jsonObject.put("provinceName", "");
			}
		}
		return executeRerutn(jsonObject);
	}

}
